/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.microweb.toastmaster.model.viewwrapper;

import com.microweb.toastmaster.model.Actor;
import com.microweb.toastmaster.model.User;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author watercup
 */
public class UserWrapper implements Comparable {
    private final Log log = LogFactory.getLog(UserWrapper.class);
    
    private String username;
    private String fullName;
    private Date agendaDate;
    private Date actorDate;
    private Date unvailableStartDate;
    private Date unvailableStopDate;
    
    public UserWrapper(User user, String tmRolename) {
        this(user, tmRolename, (Date)null);
    }
    
    public UserWrapper(User user, String tmRolename, Date agendaDate) {
        this.username = user.getUsername();
        this.fullName = user.getFullName();
        this.agendaDate = agendaDate;

        Actor actor = (Actor)user.getActorByRole(tmRolename);
        // System.out.println(actor);
        if (actor != null && actor.getAgenda() != null) 
            this.actorDate = (Date)actor.getAgenda().getDate();
        
        this.unvailableStartDate = user.getUnvailableStartDate();
        this.unvailableStopDate = user.getUnvailableStopDate();
    }
    
    public String getOptionString() {
        String result = fullName;
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
        if (actorDate != null) {
            String dateStr = format.format(actorDate);
            result +=" [" + dateStr + "]";
        }
        
        
        /*
         if (this.unvailableStartDate != null)
            result = "[X] "+ result;
         */
        
        
        if (this.unvailableStartDate != null 
                && this.agendaDate != null
                && this.unvailableStartDate.before(agendaDate)) {
           log.debug("unavilable Start date: " + this.unvailableStartDate);
           log.debug("agenda Date: " + this.agendaDate);
           if (this.unvailableStopDate == null 
                   || this.unvailableStopDate.after(agendaDate)) { 
                result = "[X] "+ result;
           }
        }
           
        return result;
    }

    public void setUnvailableStopDate(Date unvailableStopDate) {
        this.unvailableStopDate = unvailableStopDate;
    }

    public void setUnvailableStartDate(Date unvailableStartDate) {
        this.unvailableStartDate = unvailableStartDate;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public Date getUnvailableStopDate() {
        return unvailableStopDate;
    }

    public Date getUnvailableStartDate() {
        return unvailableStartDate;
    }

    public String getFullName() {
        return fullName;
    }

    
    public Date getActorDate() {
        return actorDate;
    }

 
    public void setActorDate(Date actorDate) {
        this.actorDate = actorDate;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
    public int compareTo(Object o) throws ClassCastException {
        UserWrapper uw = (UserWrapper)o;
        
        if (actorDate == null)
            return -1;
        
        if (uw.getActorDate() == null) 
            return 1;
        
        return actorDate.compareTo(uw.getActorDate());
    }
}
